using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MFramework;
using UnityEngine.EventSystems;
using UnityEngine.U2D;
using UnityEngine.UI;
using DG.Tweening;

public class Card : MonoBehaviour
{
    public int colorStyle;
    public int shapeStyle;
    public int countStyle;

    public int row;
    public int col;

    private UIEventListener m_listener;
    private Image m_bgSelect;
    private Image m_image1;
    private Image m_image2;
    private Image m_image3;

    private UIGame m_uiGame;

    private AssetHandle m_altasHandle;

    private void Awake()
    {
        m_uiGame = FindAnyObjectByType<UIGame>();
        m_listener = GetComponentInChildren<UIEventListener>();
        m_image1 = transform.FindChildByName("Img1").GetComponent<Image>();
        m_image2 = transform.FindChildByName("Img2").GetComponent<Image>();
        m_image3 = transform.FindChildByName("Img3").GetComponent<Image>();
        m_bgSelect = transform.FindChildByName("BgSelect").GetComponent<Image>();

        m_bgSelect.enabled = false;

        m_listener.PointerClick += OnClick;

        InitStyle();
        PlayAnim();
    }

    public bool CompleteNotSame(Card other)
    {
        return colorStyle != other.colorStyle && shapeStyle != other.shapeStyle && countStyle != other.countStyle;
    }

    public void OnUnSelect()
    {
        m_bgSelect.enabled = false;
    }

    public void OnSelect()
    {
        m_bgSelect.enabled = true;
    }

    private void InitStyle()
    {
        colorStyle = Random.Range(1, 4);
        shapeStyle = Random.Range(1, 4);
        countStyle = Random.Range(1, 4);

        InitColor();
        InitShape();
        InitCount();
    }

    private void InitColor()
    {
        if (colorStyle == 1)
        {
            m_image1.color = Color.red;
            m_image2.color = Color.red;
            m_image3.color = Color.red;
        }
        else if (colorStyle == 2)
        {
            m_image1.color = Color.blue;
            m_image2.color = Color.blue;
            m_image3.color = Color.blue;
        }
        else
        {
            m_image1.color = Color.green;
            m_image2.color = Color.green;
            m_image3.color = Color.green;
        }
    }

    private void InitShape()
    {
        m_altasHandle = ResourcesManager.LoadAsset<SpriteAtlas>("Assets/GameRes/UIAltas/Altas");
        var altas = m_altasHandle.AssetObject as SpriteAtlas;

        if (shapeStyle == 1)
        {
            m_image1.sprite = altas.GetSprite("Circle");
            m_image2.sprite = altas.GetSprite("Circle");
            m_image3.sprite = altas.GetSprite("Circle");
        }
        else if (shapeStyle == 2)
        {
            m_image1.sprite = altas.GetSprite("Square");
            m_image2.sprite = altas.GetSprite("Square");
            m_image3.sprite = altas.GetSprite("Square");
        }
        else
        {
            m_image1.sprite = altas.GetSprite("Triangle");
            m_image2.sprite = altas.GetSprite("Triangle");
            m_image3.sprite = altas.GetSprite("Triangle");
        }
    }

    private void InitCount()
    {
        if (countStyle == 1)
        {
            m_image1.gameObject.SetActive(false);
            m_image3.gameObject.SetActive(false);
        }
        else if (countStyle == 2)
        {
            m_image2.gameObject.SetActive(false);
            (m_image1.transform as RectTransform).anchoredPosition = new Vector2(0, 15);
            (m_image3.transform as RectTransform).anchoredPosition = new Vector2(0, -15);
        }
    }

    private void PlayAnim()
    {
        transform.localScale = Vector3.one * 0.01f;
        transform.DOScale(1.5f, 0.3f);
    }

    private void OnClick(PointerEventData eventData)
    {
        m_uiGame.TrySelectCard(this);
    }

    private void OnDestroy()
    {
        if(m_altasHandle != null)
        {
            m_altasHandle.Release();
            m_altasHandle = null;
        }
    }
}
